var API_URL = 'https://wenzhe.xyz/wechatApi/'

var requestHandler = {
  params: {},
  url: '',
  success: function (res) {
    // success  
  },
  fail: function () {
    // fail  
  },
}

//GET请求  
function GET(requestHandler) {
  request('GET', requestHandler)
}
//POST请求  
function POST(requestHandler) {
  requestHandler.headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json',
  }
  request('POST', requestHandler)
}

function request(method, requestHandler) {
  //注意：可以对params加密等处理 
  var headers = requestHandler.headers
  var params = requestHandler.params;
  if (requestHandler.url === undefined) {
    requestHandler.url = ''
  }
  var RequestUrl = API_URL + requestHandler.url

  wx.request({
    url: RequestUrl,
    data: params,
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
    header: headers, // 设置请求的 header  
    success: function (res) {
      //注意：可以对参数解密等处理 
      if (res.statusCode != 200) {
        requestHandler.fail()
        return
      }
      requestHandler.success(res)
    },
    fail: function () {
      requestHandler.fail()
    },
    complete: function () {
      // complete  
    }
  })
}

module.exports = {
  GET: GET,
  POST: POST
}  